package org.commonsemantics.scigrails.module.users

import org.commonsemantics.scigrails.module.users.group.Group 

/**
 * Object command for Group validation and creation.
 * 
 * @author Paolo Ciccarese <paolo.ciccarese@gmail.com>
 */
class GroupCreateCommand {
	
	public static final Integer NAME_MAX_SIZE = 255;
	public static final Integer SHORTNAME_MAX_SIZE = 16;
	public static final Integer DESCRIPION_MAX_SIZE = 1024;
	
	String name;
	String shortName;
	String description;
	
	String status;
	
	static constraints = {
		name (nullable:false, blank: false, maxSize:NAME_MAX_SIZE)
		shortName (nullable:false, blank: false, maxSize:SHORTNAME_MAX_SIZE)
		description (nullable:false, blank:true, maxSize:DESCRIPION_MAX_SIZE)
	}
	
	boolean isEnabled() {
		return status.equals(GroupStatus.ACTIVE_GROUP.value);
	}
	
	boolean isLocked() {
		return status.equals(GroupStatus.LOCKED_GROUP.value);
	}
	
	Group createGroup() {
		// Names and nicknames are supposed to be unique
		if(Group.findByName(name)!=null || Group.findByShortName(shortName)!=null) return null;
		// If the group does not exist I create a new one
		else return new Group(name:name, shortName:shortName, description:description, 
			enabled: true, locked: false);
	}
}
